/**
 * Visualization tool for Hokuyo URG-04LX Laser Sensor.
 * Copyright 2007 - Brian Ziman [bziman(at)acm.org]
 * http://www.brianziman.com/
 */
package fr.imag.m2pgi.m2m.geoloc.scip;

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.io.IOException;

import java.util.*;

/*  ^ 8192
 *  |        0
 *  | 90     *     -90
 *  |  120       -120
 *  v <----8192------->
 */
public class LaserVisualizer2 extends JPanel {

    double xmin = -4096;
    double xmax = 4096;
    double ymin = -4096;
    double ymax = 4096;

    private SCIP20 gLaser;
    private JFrame frame;

    private static boolean useLog = false;

    public static void main(String args[]) throws Exception {
        int port = 1701;
        for(int i=0;i<args.length;i++) {
            if(args[i].startsWith("-l")) {
                useLog = true;
            } else {
                port = Integer.parseInt(args[i]);
            }
        }
        LaserVisualizer2 lv = new LaserVisualizer2(new SCIP20(port));
        for(;;) {
            lv.repaint();
            Thread.sleep(200);
        }
    }

    public LaserVisualizer2(SCIP20 laser) {
        this.gLaser = laser;
//        try {
//          gLaser.enableLaser();
//        } catch (IOException e1) {
//          // TODO Auto-generated catch block
//          e1.printStackTrace();
//        }
//        setDoubleBuffered(true);
        frame = new JFrame();
        frame.addWindowListener(new WindowAdapter() { 
            public void windowClosing(WindowEvent e) { 
                System.exit(0); 
            }
        });
        frame.setTitle("Laser GUI");
        //setBounds(0,0,800,800);
        frame.setSize(800,800);
        frame.add(this);
        frame.setVisible(true);
    }

    Font font = new Font("FreeMono", Font.BOLD, 50);

    double maxrange = 0;
    public void paint(Graphics g) {
      try {
        ArrayList<Integer> data = gLaser.getTwoCharLastMeasure();
        g.setColor(Color.WHITE);
        g.fillRect(0,0,getWidth(),getHeight());

        g.setFont(font);
        g.setColor(Color.BLACK);
        try {
          g.drawString("Range(0): "+gLaser.getDistance(data,0d), 20,70);
        } catch (IOException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
        }

        double scale = (getHeight()-300)/4096d;
        int step = (getWidth()-100)/data.size();
        
        int x=50;
        for(int i=0; i<data.size(); i++) {
          int val = data.get(i);
          if(val==0) {
            g.setColor(Color.RED);
            g.drawLine(x, getHeight(), x, 0);
          } else {
            if(i%10==0)
              g.setColor(Color.BLACK);
            else
              g.setColor(Color.GREEN);
            g.drawLine(x, getHeight(), x, getHeight()-(int)((double)val*scale));
          }
          x+=step;
        }
      } catch(IOException e) {
        e.printStackTrace();
      }
    }

    // draw a cirle of radius r centered at x, y
    // Normally, y is increasing as it goes down the screen.
    // We want to reverse that, so we can pass in normal
    // coordinates.
    private void fillCircle(Graphics g, int x, int y, int r) {
        int topleftx = x - r/2;
        int toplefty = y - r/2;
        g.fillOval(topleftx, toplefty, r, r);
    }


}
